package com.pandora.credit.mongo.dao.impl;

import com.pandora.credit.mongo.dao.H5ReportDao;
import com.pandora.credit.mongo.entity.CustomerReportCriteria;
import com.pandora.credit.mongo.entity.H5Report;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author developer
 */
@Repository("h5ReportDaoImpl")
public class H5ReportDaoImpl implements H5ReportDao {

    @Resource(name = "mongoTemplateH5")
    private MongoTemplate mongoTemplate;

    @Override
    public void insert(H5Report h5Report) {
        mongoTemplate.save(h5Report);
    }

    @Override
    public H5Report selectById(long id) {
        return mongoTemplate.findById(id, H5Report.class);
    }

    @Override
    public List<H5Report> selectCustomerReports(CustomerReportCriteria crc) {
        Query query = new Query();
        query.addCriteria(Criteria.where("customer_id").is(crc.getCustomerId()));
        query.addCriteria(Criteria.where("type").is(crc.getType()));
        if(crc.getNextId() > 0) {
            query.addCriteria(Criteria.where("_id").lte(crc.getNextId()));
        }

        query.with(new Sort(Sort.Direction.DESC, "_id"));
        query.limit(crc.getPageLength() + 1);
        List<H5Report> reports = mongoTemplate.find(query, H5Report.class);
        return reports;
    }

    @Override
    public List<H5Report> selectByCusType(long customerId, String type) {
        Query query = new Query();
        Criteria criteria = Criteria.where("customer_id").is(customerId).and("type").is(type);
        query.addCriteria(criteria);
        List<H5Report> li = mongoTemplate.find(query, H5Report.class);
        return li;
    }

}
